// miniprogram/pages/admin/service/service.js
const app = getApp();
const uploader = require('../../../utils/upload.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    app.login(user => {
      if (!user.studioid) {
        wx.navigateBack();
        return;
      }
      wx.showNavigationBarLoading();
      wx.cloud.callFunction({
        name: 'crud',
        data: {
          op: 'get',
          table: 'service',
          field: {
            // nodes: false,
            discount: false
          },
          order: {field: 'sticky', by: 'desc'},
          skip: 0,
          where: {
            studioid: user.studioid
          }
          // orderBy: [{'updatetime': desc}]
        },
        success: res => {
          that.setData({
            items: res.result.data
          })
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  showActions(e) {
    const that = this;
    const index = e.currentTarget.dataset.index; // TODO
    const actionindex = e.currentTarget.dataset.actionindex;
    const item = this.data.items[index];
    switch (actionindex) {
      case '3':
        that.removeItem(item)
        break;
      case '1':
        that.updateStatus(item);
        break;
      case '0':
        // 跳转修改
        wx.navigateTo({
          url: `../newService/newService?_id=${item._id}`,
        })
        break;
      case '2':
        // TODO 查看
        wx.navigateToMiniProgram({
          appId: app.globalData.user.studio.appid,
          envVersion: 'trial',
          path: `/pages/user/service/detail/detail?_id=${item._id}`,
        })
        break;
    }
  
  },

  updateStatus: function (item) {
    const that = this;
    wx.cloud.callFunction({
      name: 'crud',
      data: {
        op: 'update',
        table: 'service',
        data: {
          _id: item._id,
          status: item.status == 0 ? 1 : 0
        }
      },
      success: res => {
        console.log(res)
        that.onLoad()
      }
    })
  },

  removeItem: function (item) {
    const that = this;
    wx.showModal({
      title: '确认',
      content: '您确认要删除此套系？',
      success(res) {
        if (res.confirm) {
      
          wx.cloud.callFunction({
            name: 'crud',
            data: {
              op: 'remove',
              table: 'service',
              _id: item._id
            },
            success: res => {
              if (res.result.stats.removed == 1) {
                that.onLoad()
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})